# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: app_client.py
Author: kij
Create Date: 2021/2/27
-------------------------------------------------
"""
import socket
import optparse
import json
import app_interface

def auth(ip, port):
	count = 0
	while count < 3:
		user_name = input("请输入用户名:").strip()
		if not user_name:
			continue
		user_password = input("输入用户名:").strip()
		if not user_password:
			continue
		# 用于生成json数据传递给服务器
		has_account, response = app_interface.auth(ip, port, user_name, user_password)

		if not has_account:
			print(response)
			return has_account, response
		else:
			print("登录成功")
			return has_account, response
		count += 1


def runing():
	parser = optparse.OptionParser()
	parser.add_option("-s", "--server", dest="server", help="服务器IP地址")
	parser.add_option("-P", "--port", type='int', dest="port", help="服务器端口")
	parser.add_option("-u", "--username", dest="username", help="用户姓名")
	parser.add_option("-p", "--password", dest="password", help="用户密码")
	options, args = parser.parse_args()
	# 调用链接
	has_ip, hint = app_interface.argv_verification(options.server, options.port)
	if not has_ip:
		print(hint)
	else:
		is_counent, user = auth(options.server, options.port)

		if is_counent:
			while True:
				user_input = input("[%s]>>>" % user.user_name).strip()
				if not user_input: continue
				cmd_list = user_input.split()
				if "_%s" % cmd_list[0] in func_list:
					func = "_%s" % cmd_list[0]
					func_list.get(func)(cmd_list[1:], user)
				# if hasattr("_%s" % cmd_list[0]):
				# 	func = getattr("_%s" % cmd_list[0])
				# 	func(cmd_list[1:], user)


def _get(cmd_args, user=None):
	"""下载文件"""
	has_parameter, hint = app_interface.parameter_check(cmd_args, min_args=1)
	if not has_parameter:
		print(hint)
	else:
		exist, hint = app_interface.download_file(cmd_args, user)
		if not exist:
			print(hint)
		else:
			print('下载完毕')


def _ls(cmd_args, user=None):
	print(cmd_args)
	exist, hint = app_interface.show_dir(user)
	if not exist:
		print(hint)
	else:
		print('显示文件夹',hint)

def _put():
	pass


# class FTPClient(object):
# 	"""
# 	客户端交互
# 	"""
# 	MSG_SIZE = 1024
#
# 	def __init__(self):
# 		parser = optparse.OptionParser()
# 		parser.add_option("-s", "--server", dest="server", help="服务器IP地址")
# 		parser.add_option("-P", "--port", type='int', dest="port", help="服务器端口")
# 		parser.add_option("-u", "--username", dest="username", help="用户姓名")
# 		parser.add_option("-p", "--password", dest="password", help="用户密码")
# 		self.options, self.args = parser.parse_args()
# 		print(self.options, self.args)
# 		# 调用链接
# 		self.argv_verification()
# 		# 调用链接
# 		self.make_connection()
#
# 	# 校验参数是否正确
# 	def argv_verification(self):
# 		if not self.options.server or not self.options.port:
# 			print('IP地址和端口未输入')
#
# 	def make_connection(self):
# 		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 		self.sock.connect((self.options.server, self.options.port))
#
# 	def auth(self):
# 		"""用户认证"""
# 		count = 0
# 		while count < 3:
# 			user_name = input("输入用户名:").strip()
# 			if not user_name: continue
# 			user_password = input("输入用户密码:").strip()
# 			# 用于生成json数据传递给服务器
# 			cmd = {
# 				'action_type': 'auth',
# 				'username': user_name,
# 				'password': user_password,
# 			}
# 			# 发送数据
# 			self.sock.send(json.dumps(cmd).encode('utf-8'))
# 			# 接收服务器数据
# 			response = self.get_response()
# 			print("response:", response)
# 			if response.get('status-code') == 200:
# 				pass
# 			else:
# 				print(response.get('status_msg'))
# 			count += 1
#
# 	def interactive(self):
# 		"""处理所有ftpsercer交互"""
# 		if self.auth():
# 			pass
#
# 	def get_response(self):
# 		"""获取服务器信息"""
# 		data = self.sock.recv(self.MSG_SIZE)
# 		return json.loads(data.decode('utf-8'))



func_list = {
	"_get": _get,
	"_ls": _ls,
}

if __name__ == "__main__":
	runing()
